/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * BaseOperateRecordController
 * 操作记录
 * 修改纪录
 * 2020-10-30 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-10-30
 */
package com.topscomm.base.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.controller.auto.BaseOperateRecordControllerAuto;
import com.topscomm.base.pojo.BaseOperateRecordEntity;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: zhanglongfei
 * @date: 2020-10-30
 * @description:操作记录Controller
 * @modify:
 */
@RestController
@RequestMapping("base/operateRecord")
public class BaseOperateRecordController extends BaseOperateRecordControllerAuto
		implements IBaseOperateRecordController {
	/**
	 * @author: zhanglongfei
	 * @date: 2020-10-30
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourcetype")) {
			String value = paraMap.get("qry_sourcetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourcetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceid")) {
			String value = paraMap.get("qry_sourceid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourceid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_operatetype")) {
			String value = paraMap.get("qry_operatetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and operatetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_operateresult")) {
			String value = paraMap.get("qry_operateresult").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and operateresult = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_usercode")) {
			String value = paraMap.get("qry_usercode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and usercode = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: zhanglongfei
	 * @date: 2020-10-30
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description:类似关注、收藏等操作，第一次调用是增加，第二次取消，第三次增加...
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年10月30日下午4:17:31
	 * @modify:
	 */
	@PostMapping(value = "operateState")
	public ResponseResult<Object> operateState(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendLineSingle("sourcetype='{0}' and sourceid='{1}'", paraMap.get("sourcetype"),
					paraMap.get("sourceid"));
			sql.appendLineSingle("and operatetype='{0}' and usercode='{1}'", paraMap.get("operatetype"),
					this.getUserBean().getUser().getCode());
			int count = this.service.getCount(sql.toString());
			if (count > 0) {
				this.service.deleteByWhere(sql.toString());
			} else {
				paraMap.put(BaseOperateRecordEntity.FieldUsercode, this.getUserBean().getUser().getCode());
				paraMap.put(BaseOperateRecordEntity.FieldUsername, this.getUserBean().getUser().getName());
				paraMap.put(BaseOperateRecordEntity.FieldOperatedate, new Date());
				insert(paraMap);
			}
			return ResponseResult.ok(count > 0 ? "off" : "on");
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/***
	 * @description:查询列表
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年11月9日上午11:37:12
	 * @modify:
	 */
	@GetMapping(value = "queryList")
	public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			if (paraMap.containsKey("order")) {
				sqlWhere += " order by " + paraMap.get("order");
			}
			List<BaseOperateRecordEntity> list = this.service.queryByWhere(sqlWhere);
			return ResponseResult.ok(list);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}
}
